package top.v5it.japi.ddd.example.account.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import top.v5it.japi.ddd.core.dao.impl.BasicDaoJdbcImpl;
import top.v5it.japi.ddd.example.account.entity.ObuBlackList1;

import java.io.BufferedReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanpu
 * @date 2024/1/2
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ObuBlackListDaoTest1 {

    private static final Log log = LogFactory.get();
    @Autowired
    private BasicDaoJdbcImpl basicDaoJdbc;

    private static final String FILENAME = "/home/bruce/test/BASIC_OBUBLACKLISTDOWN_RES_15_20200313031818379.txt";

    @Test
    public void test_save() {
        List<ObuBlackList1> current = new ArrayList<>(1000);
        int counter = 0;
        boolean first = true;
        try (final BufferedReader reader = FileUtil.getUtf8Reader(FILENAME)) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (first) {
                    first = false;
                    continue;
                }
                final String[] arr = StrUtil.splitToArray(line, "\t");
                final ObuBlackList1 obuBlackList1 = new ObuBlackList1()
                        .setIssuer_id(arr[0])
                        .setCreation_time(LocalDateTime.parse(arr[1]))
                        .setType(Integer.valueOf(arr[2]))
                        .setObu_id(arr[3])
                        .setStatus(Integer.valueOf(arr[4]))
                        .setVersion(arr[5]);

                current.add(obuBlackList1);
                counter++;
                if (counter % 1000 == 0) {
                    log.info("{}", current.size());
                    basicDaoJdbc.jdbcInsertForList(current);
                    current.clear();
                }
            }
            basicDaoJdbc.jdbcInsertForList(current);
            current.clear();
        } catch (Exception ignore) {

        }
    }
}
